/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import types.DoubleXML;
import types.IDataXML;
import types.IntegerXML;
import types.ListXML;
import types.StringXML;

/**
 *
 * @author efim
 */
public class XMLRequester {
    public OutputStream Request(InputStream is)
    {
        try {
            SAXBuilder builder = new SAXBuilder();
            Document doc = (Document) builder.build(is);
            Element root = doc.getRootElement();
            Element m = root.getChild("method");            
            Element d = root.getChild("IDataXML");            
            IDataXML data = parseElement(d);
            switch(m.getText())
            {
                case "SELECT":
                    
                    break;
                case "INSERT":
                    
                    break;
                case "DELETE":
                    
                    break;
            }
        } catch (JDOMException ex) {
            Logger.getLogger(XMLRequester.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XMLRequester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private IDataXML parseElement(Element root)
    {
        String name = root.getName();
        IDataXML result = null;
        switch (name) {
            case DoubleXML.TYPE:
                result = new DoubleXML(Double.parseDouble(root.getValue()));
                break;
            case IntegerXML.TYPE:
                result = new IntegerXML(Integer.parseInt(root.getValue()));
                break;
            case StringXML.TYPE:
                result = new StringXML(root.getValue());
                break;
            case ListXML.TYPE:
                List<Element> childs = root.getChildren();
                LinkedList<IDataXML> ilist = new LinkedList<>();
                for(Element c:childs)
                {
                    ilist.addLast(parseElement(c));
                }
                result = new ListXML(ilist);
                break;
        }
        return result;
    }
}
